package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.factorydata.domain.SupplierData;
import com.ruoyi.factorydata.domain.dto.CustomerDataDTO;
import com.ruoyi.factorydata.domain.vo.CustomerDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.CustomerData;
import com.ruoyi.factorydata.service.ICustomerDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户数据Controller
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/customerdata/customerdata")
public class CustomerDataController extends BaseController
{
    @Autowired
    private ICustomerDataService customerDataService;

    /**
     * 查询客户数据列表
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CustomerDataDTO customerDataDTO)
    {
        startPage();
        List<CustomerDataVo> list = customerDataService.selectCustomerDataList(customerDataDTO);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 导出客户数据列表
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:export')")
    @Log(title = "客户数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerDataDTO customerDataDTO)
    {
        List<CustomerDataVo> list = customerDataService.selectCustomerDataList(customerDataDTO);
        ExcelUtil<CustomerDataVo> util = new ExcelUtil<CustomerDataVo>(CustomerDataVo.class);
        util.exportExcel(response, list, "客户数据数据");
    }

    /**
     * 获取客户数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(customerDataService.selectCustomerDataById(id));
    }

    /**
     * 新增客户数据
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:add')")
    @Log(title = "客户数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerDataDTO customerDataDTO)
    {
        boolean exists = customerDataService.lambdaQuery()
                .eq(customerDataDTO.getCustomerCode() !=  null,CustomerData::getCustomerCode, customerDataDTO.getCustomerCode()).count() > 0;
        if (exists){
            return error("客户编码已存在");
        }
        boolean exists1 = customerDataService.lambdaQuery()
                .eq(customerDataDTO.getTaxId() !=  null,CustomerData::getTaxId, customerDataDTO.getTaxId()).count() > 0;
        if (exists1){
            return error("客户税号已存在");
        }
        customerDataDTO.setCreateBy(getUserId().toString());
        return toAjax(customerDataService.insertCustomerData(customerDataDTO));
    }

    /**
     * 修改客户数据
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:edit')")
    @Log(title = "客户数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerDataDTO customerDataDTO)
    {
        customerDataDTO.setUpdateBy(getUserId().toString());
        return toAjax(customerDataService.updateCustomerData(customerDataDTO));
    }

    /**
     * 删除客户数据
     */
    @PreAuthorize("@ss.hasPermi('customerdata:customerdata:remove')")
    @Log(title = "客户数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerDataService.deleteCustomerDataByIds(ids));
    }
}
